//
//  NewFectureViewController.swift
//  SinaWeibo
//
//  Created by gaoliang on 15/11/12.
//  Copyright © 2015年 gaoliang. All rights reserved.
//

import UIKit


private let reuseIdentifier = "Cell"

let imageCount = 4
class NewFectureViewController: UICollectionViewController {
    
    
    
    // sb 会自动注册 cell 并指定 layout
    //  重写集合视图控制器的构造方法
    /*
        1.让外界调用更加简单 不需要传参数
        2,layout 和视图的大小比较固定
    */
    init() {
        
        // 实例化 layout
        let layout = UICollectionViewFlowLayout()
        // 1.指定 layout 的 itemsSize cell在实例化的时候会使用这个 size
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        super.init(collectionViewLayout: layout)
        
        // 设置分页滚动
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        
        // Register cell classes
        self.collectionView!.registerClass(NewFeactureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeactureCell

        cell.index = indexPath.item
        return cell
    }
    //MARK:停止滚动的协议方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / screenW)
        if page == imageCount - 1 {
            // 开始动画
            if let cell = collectionView?.visibleCells().last as? NewFeactureCell {
                cell.startAnimation()
            }
        }
    }
}


class NewFeactureCell: UICollectionViewCell {
    //MARK: 显示图片的新特性
    var index: Int = 0 {
        didSet {
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            
        }
    }
    //MARK: 按钮的点击事件
    @objc private func start() {
        print("用我吧")
        NSNotificationCenter.defaultCenter().postNotificationName(AppSwitchRootViewControler, object: nil)
    }
    
    //MARK: 开始执行动画效果
    private func startAnimation(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
      // 执行到最后一张新特性
                print("ok")
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 2. 设置 UI, 布局
    private func setupUI() {
        // 1.添加子视图
       contentView.addSubview(iconImage)
       contentView.addSubview(startButton)
        // 设置约束
        // edges 表示相对于 四周约束
       iconImage.snp_makeConstraints { (make) -> Void in
        make.edges.equalTo(contentView.snp_edges)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
            make.centerX.equalTo(contentView.snp_centerX)
        }
        
        // 设置点击事件
        startButton.addTarget(self, action: "start", forControlEvents: .TouchUpInside)
        startButton.hidden = true
    }

    
    // 3.实例化子视图
    private lazy var iconImage: UIImageView = UIImageView()
    // 开始按钮
   
    private lazy var startButton: UIButton = UIButton (title: "开始体验", backImage: "new_feature_finish_button", color: nil)
}
